﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDX;
using SharpDX.Direct2D1;
using Write = SharpDX.DirectWrite;

namespace SFSpacefight
{
	public class ChooserBox : Control
	{
		protected DrawingRectangleF textlayoutrect, shiplayoutrect, labellayoutrect;
		protected int currentship = 0; //only cycles through 3 starting ships
		protected int currentindex, oldindex;

		protected Label shipinfolabel;
		protected string shipinfotext;

		public ChooserBox(RenderTarget target, String intext, int x, int y, int width, int height)
			: base(target)
		{
			text = intext;
			shipinfotext = Methods.shipinfos[currentship].name + Environment.NewLine
				+ Methods.shipinfos[currentship].description + Environment.NewLine
				+ "Speed : " + Methods.shipinfos[currentship].speed + Environment.NewLine
				+ "Thrust : " + Methods.shipinfos[currentship].thrust + Environment.NewLine
				+ "Mass : " + Methods.shipinfos[currentship].mass;
			layoutrect = new DrawingRectangleF(x, y, width, height);
			textlayoutrect = new DrawingRectangleF(x + 100, y, width - 200, 30);
			shiplayoutrect = new DrawingRectangleF(x + 200, y + 150, 200, 200);
			labellayoutrect = new DrawingRectangleF(x + width / 2 - 150, y + 50, 300, 100);
		}
		public override GameState Loop(int mousex, int mousey, bool clicked)
		{ //TODO remove blinking/click variables
			Boolean thisclicked = false;
			oldalpha = alpha;
			oldindex = currentindex;

			currentindex += (currentindex + 1 == Methods.shipbitmaps[currentship].Count) ? -currentindex : 1;

			if (layoutrect.X <= mousex && layoutrect.X + layoutrect.Width >= mousex && layoutrect.Y <= mousey && layoutrect.Y + layoutrect.Height >= mousey)
				alpha = 0.9F + alpha * 0.1F;
			else
				alpha *= 0.4f;

			return thisclicked ? GameState.ButtonClicked : GameState.None;
		}
		public override void Draw(RenderTarget target, double percent)
		{
			Matrix3x2 backuptransform = target.Transform;
			float curalpha = Methods.linstep(oldalpha, alpha, percent);

			//mouseaway layer
			lightbrush.Opacity = mediumbrush.Opacity = darkbrush.Opacity = 1 - curalpha;

			target.FillRectangle(textlayoutrect, darkbrush);
			target.DrawRectangle(layoutrect, darkbrush, 1);
			target.DrawText(text, format, textlayoutrect, mediumbrush);
			target.DrawBitmap(Methods.shipbitmaps[currentship][oldindex], shiplayoutrect, (float)(1 - percent * percent), BitmapInterpolationMode.Linear);
			target.DrawBitmap(Methods.shipbitmaps[currentship][currentindex], shiplayoutrect, (float)percent, BitmapInterpolationMode.Linear);
			target.FillRectangle(labellayoutrect, darkbrush);
			target.DrawText(Methods.shipinfos[currentship].name, format, labellayoutrect, mediumbrush);

			// mouseover layer
			lightbrush.Opacity = mediumbrush.Opacity = darkbrush.Opacity = curalpha;

			target.FillRectangle(textlayoutrect, mediumbrush);
			target.DrawRectangle(layoutrect, mediumbrush, 1);
			target.DrawText(text, format, textlayoutrect, lightbrush);
			target.FillRectangle(labellayoutrect, darkbrush);
			target.DrawText(shipinfotext, format, labellayoutrect, mediumbrush);

			target.Transform = backuptransform;
		}
	}
}
